package com.ruoyi.common.core.domain;

import io.swagger.annotations.ApiModelProperty;

public class ResponseModel<T>{

    @ApiModelProperty("响应数据")
    private T data ;
    @ApiModelProperty("响应状态")
    private int code;
    @ApiModelProperty("错误信息")
    private String msg;

    public ResponseModel() {}

    public ResponseModel(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ResponseModel(T data) {
        this.data = data;
        this.code = ResponseCode.success.getCode();
        this.msg = ResponseCode.success.getMsg();
    }

    public ResponseModel(int code) {
        this.code = code;
    }

    public ResponseModel(ResponseCode code, T data) {
        this.data = data;
        this.code = code.getCode();
        this.msg = code.getMsg();
    }

    public T getData() {
        return data;
    }

    public int getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public void setData(T data) {
        this.data = data;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public static <T> ResponseModel<T> of(T data) {
        return new ResponseModel<T>(data);
    }

    public static <T> ResponseModel<T> of(ResponseCode code, T data) {
        return new ResponseModel<>(code, data);
    }

    public static <T> ResponseModel<T> ok() {
        return new ResponseModel<>(ResponseCode.success.getCode());
    }

    public static <T> ResponseModel<T> error() {
        return new ResponseModel<>(ResponseCode.normally_error.getCode(), ResponseCode.normally_error.getMsg());
    }

    public static <T> ResponseModel<T> error(String msg) {
        return new ResponseModel<>(ResponseCode.normally_error.getCode(), msg);
    }
}
